package com.leetcode.tree;

import com.study.common.TreeNode;

import java.util.LinkedList;
import java.util.List;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-11-19 10:22
 * @version: 1.0
 * @description: 对称二叉树
 * 给你一个二叉树的根节点 root ， 检查它是否轴对称。
 **/
public class SymmetryTree {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        TreeNode left,right;
        LinkedList<TreeNode> list = new LinkedList<>();
        list.addLast(root.left);
        list.addLast(root.right);

        while (!list.isEmpty()){
            left = list.pollFirst();
            right = list.pollFirst();

            if (left == null && right == null){
                continue;
            }

            if (left == null || right == null){
                return false;
            }

            if (left.val != right.val){
                return false;
            }

            list.addLast(left.left);
            list.addLast(right.right);

            list.addLast(left.right);
            list.addLast(right.left);

        }

        return true;
    }
}
